import merge from 'ts-deepmerge'
import { reactive } from 'vue'
import coreConfig from './config.core.yml'

let __config = reactive({})

/**
 * 获取框架的核心配置
 * @returns
 */
export function getCoreConfig() {
  return coreConfig as CoreConfig
}

/**
 * 合并核心配置+项目配置，返回合并后的配置
 * @returns {T & CoreConfig}
 */
export default function useConfig<T = ProjectConfig>() {
  return __config as T & CoreConfig
}

/**
 * 初始化配置
 * @param localConfig 项目内的配置文件
 */
export function initConfig<T>(localConfig: T) {
  const configMerged = merge.withOptions(
    { mergeArrays: false },
    coreConfig, // 框架核心配置
    localConfig, // 项目内的配置
    { web: window.NODE_CONFIG || {} }, // node远端配置
  )
  __config = reactive(configMerged || {})
  return __config as T & CoreConfig
}
